import BaseModel from '../BaseModel'
import { DateFormat } from 'yb-tool'
import WebBaseModel from "../web/BaseModel";
import {get, deleteAjax} from "../../api/ajax";
/**
 * 模型基础类，必须继承才能使用
 */
class LogBaseModel extends BaseModel{

  /**
   * 构造方法
   */
  constructor() {
    super()
    /**
     * serverTime
     */
    this.date = null
    this.dateStr = null

    /**
     * 删除单个数据的URL
     */
    this.removeOneUrl = null
  }

  setDataWithJson(json) {
    super.setDataWithJson (json);
    this.dateStr = DateFormat(json.date)
  }

  /**
   * 加载列表数据
   */
  static loadModels(page, otherData = {}) {
    return new Promise((resolve, reject) => {
      get(this.listUrl, {
        page,
        ...otherData
      }).then((data) => {
        resolve(this.getModelsWithArr(data))
      }).catch((err) => {
        reject(err)
      })
    })
  }

  /**
   * 删除单个数据
   */
  removeOne() {
    return new Promise((resolve, reject) => {
      deleteAjax(this.removeOneUrl, {
        id: this._id
      }).then((data) => {
        resolve(data)
      }).catch((err) => {
        reject(err)
      })
    })
  }

  /**
   * 删除所有数据
   */
  static removeAll() {
    return new Promise((resolve, reject) => {
      deleteAjax(this.removeAllUrl).then((data) => {
        resolve(data)
      }).catch((err) => {
        reject(err)
      })
    })
  }
}

/**
 * 列表加载URL
 * @type {String}
 */
WebBaseModel.listUrl = null
/**
 * 删除所有数据的URL
 * @type {String}
 */
WebBaseModel.removeAllUrl = null

export default LogBaseModel
